import re  
  
def find_listening_pids(netstat_output):  
    # 定义一个正则表达式来匹配LISTENING状态及其后面的PID  
    # 注意：这里假设PID是行尾前的一串数字  
    pattern = r'\bLISTENING\s+(\d+)$'  
      
    # 使用findall来找到所有匹配的PID  
    pids = re.findall(pattern, netstat_output, re.MULTILINE)  
      
    return pids  
  
# 示例netstat输出  
text = """  
  TCP    0.0.0.0:7999           0.0.0.0:0              LISTENING       7320  
  TCP    127.0.0.1:7999         127.0.0.1:60570        ESTABLISHED     7320  
  TCP    127.0.0.1:7999         127.0.0.1:60572        ESTABLISHED     7320  
  TCP    127.0.0.1:60570        127.0.0.1:7999         ESTABLISHED     4536  
  TCP    127.0.0.1:60572        127.0.0.1:7999         ESTABLISHED     4536  
  TCP    [::]:7999              [::]:0                 LISTENING       7320  
"""  
def find_pid(str_):
    start_idx=str_.find("LISTENING")
    res={
        'pid':'',
        'end_idx':start_idx
    }
    str_len=len(str_)
    while res['end_idx']<str_len:
        res['end_idx']+=1
        char=str_[res['end_idx']]
        if '0' <= char <= '9':
            res['pid']+=char
        elif char=='\n':break
    return res
def find_pid_set(str_):
    set_=set()
    dct=find_pid(str_)
    while 1:
        if dct['pid']!='':
            set_.add(dct['pid']) 
            str_=str_[dct['end_idx']:]
            dct=find_pid(str_)
        else:
            break
    return set_
print(find_pid_set(text))